package com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.bean.XtAirlinesCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.bean.XtAirlinesPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtAirlinesMapper {



    class XtAirlinesMapperProvider{

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_airlines t");
                WHERE("1=1");
            }}.toString();

            if (map != null) {
                String keyname = DataTypeUtil.toNotNullString(map.get("keyname"));

                if(!DataTypeUtil.isEmptyStr(keyname)){
                    sql += " and (t.name like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += " or t.short_name like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += " or t.number_code like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += " or t.two_code like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += " or t.three_code like " + DataTypeUtil.quoteAllLikeStr(keyname);
                    sql += ")";
                }
            }
            return  sql;
        }

        public String insert(XtAirlinesPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("xt_airlines");
                VALUES("id","#{id}");
                VALUES("name","#{name}");
                VALUES("full_pinyin","#{fullPinyin}");
                VALUES("camel_char_pinyin","#{camelCharPinyin}");
                VALUES("short_name","#{shortName}");
                VALUES("short_name_pinyin","#{shortNamePinyin}");
                VALUES("short_name_camel_char_pinyin","#{shortNameCamelCharPinyin}");
                VALUES("name_en","#{nameEn}");
                VALUES("number_code","#{numberCode}");
                VALUES("two_code","#{twoCode}");
                VALUES("three_code","#{threeCode}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(XtAirlinesPojo pojo) {
            String sql= new SQL() {{
                UPDATE("xt_airlines t");
                SET("t.name = #{name}");
                SET("t.full_pinyin = #{fullPinyin}");
                SET("t.camel_char_pinyin = #{camelCharPinyin}");
                SET("t.short_name = #{shortName}");
                SET("t.short_name_pinyin = #{shortNamePinyin}");
                SET("t.short_name_camel_char_pinyin = #{shortNameCamelCharPinyin}");
                SET("t.name_en = #{nameEn}");
                SET("t.number_code = #{numberCode}");
                SET("t.two_code = #{twoCode}");
                SET("t.three_code = #{threeCode}");
                SET("t.comment = #{comment}");
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_airlines t");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String findCacheAll(Map map) {
            return new SQL() {{
                SELECT("t.id ,t.name,t.short_name,t.two_code");
                FROM("xt_airlines t ");
            }}.toString();
        }

        public String fetch(String id){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_airlines t ");
                WHERE("1=1 and t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String findUK(String name,String exceptId) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_airlines t");
                WHERE(" t.name = " + DataTypeUtil.quoteStr(name));
            }}.toString();

            if(!DataTypeUtil.isEmptyStr(exceptId)){
                sql += " and t.id != " + DataTypeUtil.quoteStr(exceptId);
            }

            return  sql;
        }

    }

    @Results(id = "xtAirlinesMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "full_pinyin", property = "fullPinyin"),
            @Result(column = "camel_char_pinyin", property = "camelCharPinyin"),
            @Result(column = "short_name", property = "shortName"),
            @Result(column = "short_name_pinyin", property = "shortNamePinyin"),
            @Result(column = "short_name_camel_char_pinyin", property = "shortNameCamelCharPinyin"),
            @Result(column = "name_en", property = "nameEn"),
            @Result(column = "number_code", property = "numberCode"),
            @Result(column = "two_code", property = "twoCode"),
            @Result(column = "three_code", property = "threeCode"),
            @Result(column = "comment", property = "comment"),
    })
    @SelectProvider(type = XtAirlinesMapperProvider.class, method = "findAll")
    List<XtAirlinesPojo> findAll(Map map);

    @SelectProvider(type = XtAirlinesMapperProvider.class, method = "findCacheAll")
    @Results(id="xtAirlinesCacheMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "short_name", property = "shortName"),
            @Result(column = "two_code", property = "twoCode")
    })
    List<XtAirlinesCachePojo> findCacheAll(Map map);

    @ResultMap(value = "xtAirlinesMapId")
    @SelectProvider(type = XtAirlinesMapperProvider.class, method = "fetch")
    XtAirlinesPojo fetch(String id);

    @Results(id = "findUKMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
    })
    @SelectProvider(type = XtAirlinesMapperProvider.class, method = "findUK")
    XtAirlinesPojo findUK(String name, String exceptId);


    @InsertProvider(type = XtAirlinesMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XtAirlinesPojo pojo);

    @UpdateProvider(type = XtAirlinesMapperProvider.class, method = "update")
    int update(XtAirlinesPojo obj);

    @DeleteProvider(type = XtAirlinesMapperProvider.class, method = "delete")
    int delete(String id);
}
